<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="https:&#x2F;&#x2F;www.ethereum.org&#x2F;token  李工翻译，转载请注明出处 代币我们将要创建一个数字代币。代币在以太坊的生态系统中能代表任何可替代（以太坊的替代与不可替代定义，两两之间可以互换不存在差异）的交易物品：金币，忠诚点数，金币券，欠条，游戏内物品，等等。因为所有的代币以一种标准方式实现了基础的功能，这同样意味着你的代币能够立马被以太坊钱包与其它任何使用该标准的客户端或者合约">
<meta property="og:type" content="article">
<meta property="og:title" content="创建你自己的加密货币">
<meta property="og:url" content="http://example.com/2021/09/29/chuang-jian-jia-mi-huo-bi/index.html">
<meta property="og:site_name" content="尼奥的乱码">
<meta property="og:description" content="https:&#x2F;&#x2F;www.ethereum.org&#x2F;token  李工翻译，转载请注明出处 代币我们将要创建一个数字代币。代币在以太坊的生态系统中能代表任何可替代（以太坊的替代与不可替代定义，两两之间可以互换不存在差异）的交易物品：金币，忠诚点数，金币券，欠条，游戏内物品，等等。因为所有的代币以一种标准方式实现了基础的功能，这同样意味着你的代币能够立马被以太坊钱包与其它任何使用该标准的客户端或者合约">
<meta property="og:locale">
<meta property="og:image" content="https://www.ethereum.org/images/tutorial/deploy-new-contract.png">
<meta property="og:image" content="https://www.ethereum.org/images/tutorial/edit-contract.png">
<meta property="og:image" content="https://www.ethereum.org/images/tutorial/function-picker.png">
<meta property="og:image" content="https://www.ethereum.org/images/tutorial/Ethereum-Wallet-Screenshot-2015-12-03-at-3.50.36-PM-10.png">
<meta property="og:image" content="https://www.ethereum.org/images/tutorial/Ethereum-Wallet-Screenshot-2015-12-03-at-3.50.36-PM-11.png">
<meta property="og:image" content="https://www.ethereum.org/images/tutorial/Screen-Shot-2015-12-03-at-9.48.15-AM.png">
<meta property="og:image" content="https://www.ethereum.org/images/tutorial/Screen-Shot-2015-12-03-at-9.44.42-AM.png">
<meta property="og:image" content="https://www.ethereum.org/images/tutorial/advanced-token-deploy.png">
<meta property="og:image" content="https://www.ethereum.org/images/tutorial/created-token.png">
<meta property="og:image" content="https://www.ethereum.org/images/tutorial/manage-central-dollar.png">
<meta property="og:image" content="https://www.ethereum.org/images/tutorial/add-token.png">
<meta property="og:image" content="https://www.ethereum.org/images/tutorial/unicorn-token.png">
<meta property="article:published_time" content="2021-09-29T01:50:51.706Z">
<meta property="article:modified_time" content="2021-09-29T02:01:24.105Z">
<meta property="article:author" content="李工">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://www.ethereum.org/images/tutorial/deploy-new-contract.png">

<link rel="canonical" href="http://example.com/2021/09/29/chuang-jian-jia-mi-huo-bi/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-Hans'
  };
</script>

  <title>创建你自己的加密货币 | 尼奥的乱码</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">尼奥的乱码</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>

  </li>
        <li class="menu-item menu-item-首页">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-编程">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>编程</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/09/29/chuang-jian-jia-mi-huo-bi/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="李工">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="尼奥的乱码">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          创建你自己的加密货币
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2021-09-29 09:50:51 / Modified: 10:01:24" itemprop="dateCreated datePublished" datetime="2021-09-29T09:50:51+08:00">2021-09-29</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p><a target="_blank" rel="noopener" href="https://www.ethereum.org/token">https://www.ethereum.org/token</a> </p>
<p><strong>李工翻译，转载请注明出处</strong></p>
<h2 id="代币"><a href="#代币" class="headerlink" title="代币"></a>代币</h2><p>我们将要创建一个数字代币。代币在以太坊的生态系统中能代表任何可替代（以太坊的替代与不可替代定义，两两之间可以互换不存在差异）的交易物品：金币，忠诚点数，金币券，欠条，游戏内物品，等等。因为所有的代币以一种标准方式实现了基础的功能，这同样意味着你的代币能够立马被以太坊钱包与其它任何使用该标准的客户端或者合约所兼容。</p>
<span id="more"></span>

<h3 id="最小可行代币"><a href="#最小可行代币" class="headerlink" title="最小可行代币"></a>最小可行代币</h3><p>标准代币合约可能会比较复杂。但本质上一个非常基础的代币可以归结为这样：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">contract MyToken &#123;</span><br><span class="line">    <span class="comment">/* This creates an array with all balances */</span></span><br><span class="line">    mapping (<span class="function"><span class="params">address</span> =&gt;</span> uint256) public balanceOf;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Initializes contract with initial supply tokens to the creator of the contract */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">MyToken</span>(<span class="params"></span></span></span><br><span class="line"><span class="params"><span class="function">        uint256 initialSupply</span></span></span><br><span class="line"><span class="params"><span class="function">        </span>) </span>&#123;</span><br><span class="line">        balanceOf[msg.sender] = initialSupply;              <span class="comment">// Give the creator all initial tokens</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Send coins */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">transfer</span>(<span class="params">address _to, uint256 _value</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">require</span>(balanceOf[msg.sender] &gt;= _value);           <span class="comment">// Check if the sender has enough</span></span><br><span class="line">        <span class="built_in">require</span>(balanceOf[_to] + _value &gt;= balanceOf[_to]); <span class="comment">// Check for overflows</span></span><br><span class="line">        balanceOf[msg.sender] -= _value;                    <span class="comment">// Subtract from the sender</span></span><br><span class="line">        balanceOf[_to] += _value;                           <span class="comment">// Add the same to the recipient</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><p>如果你想拷贝粘贴一个更加完整的代码，如下：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br></pre></td><td class="code"><pre><span class="line">pragma solidity ^<span class="number">0.4</span><span class="number">.16</span>;</span><br><span class="line"></span><br><span class="line">interface tokenRecipient &#123; <span class="function"><span class="keyword">function</span> <span class="title">receiveApproval</span>(<span class="params">address _from, uint256 _value, address _token, bytes _extraData</span>) <span class="title">public</span></span>; &#125;</span><br><span class="line"></span><br><span class="line">contract TokenERC20 &#123;</span><br><span class="line">    <span class="comment">// Public variables of the token</span></span><br><span class="line">    string public name;</span><br><span class="line">    string public symbol;</span><br><span class="line">    uint8 public decimals = <span class="number">18</span>;</span><br><span class="line">    <span class="comment">// 18 decimals is the strongly suggested default, avoid changing it</span></span><br><span class="line">    uint256 public totalSupply;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// This creates an array with all balances</span></span><br><span class="line">    mapping (<span class="function"><span class="params">address</span> =&gt;</span> uint256) public balanceOf;</span><br><span class="line">    mapping (<span class="function"><span class="params">address</span> =&gt;</span> mapping (<span class="function"><span class="params">address</span> =&gt;</span> uint256)) public allowance;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// This generates a public event on the blockchain that will notify clients</span></span><br><span class="line">    event Transfer(address indexed <span class="keyword">from</span>, address indexed to, uint256 value);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// This notifies clients about the amount burnt</span></span><br><span class="line">    event Burn(address indexed <span class="keyword">from</span>, uint256 value);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Constructor function</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * Initializes contract with initial supply tokens to the creator of the contract</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">TokenERC20</span>(<span class="params"></span></span></span><br><span class="line"><span class="params"><span class="function">        uint256 initialSupply,</span></span></span><br><span class="line"><span class="params"><span class="function">        string tokenName,</span></span></span><br><span class="line"><span class="params"><span class="function">        string tokenSymbol</span></span></span><br><span class="line"><span class="params"><span class="function">    </span>) <span class="title">public</span> </span>&#123;</span><br><span class="line">        totalSupply = initialSupply * <span class="number">10</span> ** uint256(decimals);  <span class="comment">// Update total supply with the decimal amount</span></span><br><span class="line">        balanceOf[msg.sender] = totalSupply;                <span class="comment">// Give the creator all initial tokens</span></span><br><span class="line">        name = tokenName;                                   <span class="comment">// Set the name for display purposes</span></span><br><span class="line">        symbol = tokenSymbol;                               <span class="comment">// Set the symbol for display purposes</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Internal transfer, only can be called by this contract</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">_transfer</span>(<span class="params">address _from, address _to, uint _value</span>) <span class="title">internal</span> </span>&#123;</span><br><span class="line">        <span class="comment">// Prevent transfer to 0x0 address. Use burn() instead</span></span><br><span class="line">        <span class="built_in">require</span>(_to != <span class="number">0x0</span>);</span><br><span class="line">        <span class="comment">// Check if the sender has enough</span></span><br><span class="line">        <span class="built_in">require</span>(balanceOf[_from] &gt;= _value);</span><br><span class="line">        <span class="comment">// Check for overflows</span></span><br><span class="line">        <span class="built_in">require</span>(balanceOf[_to] + _value &gt; balanceOf[_to]);</span><br><span class="line">        <span class="comment">// Save this for an assertion in the future</span></span><br><span class="line">        uint previousBalances = balanceOf[_from] + balanceOf[_to];</span><br><span class="line">        <span class="comment">// Subtract from the sender</span></span><br><span class="line">        balanceOf[_from] -= _value;</span><br><span class="line">        <span class="comment">// Add the same to the recipient</span></span><br><span class="line">        balanceOf[_to] += _value;</span><br><span class="line">        Transfer(_from, _to, _value);</span><br><span class="line">        <span class="comment">// Asserts are used to use static analysis to find bugs in your code. They should never fail</span></span><br><span class="line">        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Transfer tokens</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * Send `_value` tokens to `_to` from your account</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_to The address of the recipient</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_value the amount to send</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">transfer</span>(<span class="params">address _to, uint256 _value</span>) <span class="title">public</span> </span>&#123;</span><br><span class="line">        _transfer(msg.sender, _to, _value);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Transfer tokens from other address</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * Send `_value` tokens to `_to` on behalf of `_from`</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_from The address of the sender</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_to The address of the recipient</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_value the amount to send</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">transferFrom</span>(<span class="params">address _from, address _to, uint256 _value</span>) <span class="title">public</span> <span class="title">returns</span> (<span class="params">bool success</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">require</span>(_value &lt;= allowance[_from][msg.sender]);     <span class="comment">// Check allowance</span></span><br><span class="line">        allowance[_from][msg.sender] -= _value;</span><br><span class="line">        _transfer(_from, _to, _value);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Set allowance for other address</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * Allows `_spender` to spend no more than `_value` tokens on your behalf</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_spender The address authorized to spend</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_value the max amount they can spend</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">approve</span>(<span class="params">address _spender, uint256 _value</span>) <span class="title">public</span></span></span><br><span class="line"><span class="function">        <span class="title">returns</span> (<span class="params">bool success</span>) </span>&#123;</span><br><span class="line">        allowance[msg.sender][_spender] = _value;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Set allowance for other address and notify</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * Allows `_spender` to spend no more than `_value` tokens on your behalf, and then ping the contract about it</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_spender The address authorized to spend</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_value the max amount they can spend</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_extraData some extra information to send to the approved contract</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">approveAndCall</span>(<span class="params">address _spender, uint256 _value, bytes _extraData</span>)</span></span><br><span class="line"><span class="function">        <span class="title">public</span></span></span><br><span class="line"><span class="function">        <span class="title">returns</span> (<span class="params">bool success</span>) </span>&#123;</span><br><span class="line">        tokenRecipient spender = tokenRecipient(_spender);</span><br><span class="line">        <span class="keyword">if</span> (approve(_spender, _value)) &#123;</span><br><span class="line">            spender.receiveApproval(msg.sender, _value, <span class="built_in">this</span>, _extraData);</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Destroy tokens</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * Remove `_value` tokens from the system irreversibly</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_value the amount of money to burn</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">burn</span>(<span class="params">uint256 _value</span>) <span class="title">public</span> <span class="title">returns</span> (<span class="params">bool success</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">require</span>(balanceOf[msg.sender] &gt;= _value);   <span class="comment">// Check if the sender has enough</span></span><br><span class="line">        balanceOf[msg.sender] -= _value;            <span class="comment">// Subtract from the sender</span></span><br><span class="line">        totalSupply -= _value;                      <span class="comment">// Updates totalSupply</span></span><br><span class="line">        Burn(msg.sender, _value);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Destroy tokens from other account</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * Remove `_value` tokens from the system irreversibly on behalf of `_from`.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_from the address of the sender</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_value the amount of money to burn</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">burnFrom</span>(<span class="params">address _from, uint256 _value</span>) <span class="title">public</span> <span class="title">returns</span> (<span class="params">bool success</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">require</span>(balanceOf[_from] &gt;= _value);                <span class="comment">// Check if the targeted balance is enough</span></span><br><span class="line">        <span class="built_in">require</span>(_value &lt;= allowance[_from][msg.sender]);    <span class="comment">// Check allowance</span></span><br><span class="line">        balanceOf[_from] -= _value;                         <span class="comment">// Subtract from the targeted balance</span></span><br><span class="line">        allowance[_from][msg.sender] -= _value;             <span class="comment">// Subtract from the sender&#x27;s allowance</span></span><br><span class="line">        totalSupply -= _value;                              <span class="comment">// Update totalSupply</span></span><br><span class="line">        Burn(_from, _value);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="理解代码"><a href="#理解代码" class="headerlink" title="理解代码"></a>理解代码</h3><p><img src="https://www.ethereum.org/images/tutorial/deploy-new-contract.png" alt="image"></p>
<p>让我们开始基础部分。打开钱包应用，去到<em>合约</em>页面然后点击<em>部署新合约</em>。在<em>SOLIDITY合约原始代码</em>，输入以下代码：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">contract MyToken &#123;</span><br><span class="line">    <span class="comment">/* This creates an array with all balances */</span></span><br><span class="line">    mapping (<span class="function"><span class="params">address</span> =&gt;</span> uint256) public balanceOf;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>mapping映射关键字是指一个关联数组，用来关联用户地址跟余额。address地址是以太坊的基础十六进制格式，余额是整形数，范围从0到115quattuorvigintillion（乘以10的75次方）。如果你不知道quattuorvigintillion有多少，是很多vigintillions反正比你计划使用的代币总数多得多。public关键字，意味着这个变量可以被区块链上的任何人访问到，意味着所有的余额都是公开的（需要这样做，这样所有的客户端都可以显示这些数量）。</p>
<p><img src="https://www.ethereum.org/images/tutorial/edit-contract.png" alt="image"></p>
<p>如果你马上发布你的合约，它将开始工作但不会那么有效果：它是一个能够查询任何保存了你的代币余额的地址的合约，但你还没有创建任何一个代币，所以数组里的任何一组值都将返回0。所以我们将在合约启动的时候创建一些代币。在mapping下添加这些代码：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">MyToken</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    balanceOf[msg.sender] = <span class="number">21000000</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意MyToken函数拥有跟合约一样的名称MyToken。这是非常重要的，如果你想重命名其中的一个，另一个名字也必须重新命名：这是很特别的，启动函数只运行一次，当且仅当合约被第一次上传发布到网络中。这个函数会设置msg.sender地址的余额数，msg.sender就是部署这个合约的用户，余额被设置为2100万。</p>
<p>选择2100万是随意的，在代码中你自己能改成任意数目，但这儿有一个更好的方式：将这个数量作为函数的参数传进来，就像这样：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">MyToken</span>(<span class="params">uint256 initialSupply</span>) <span class="title">public</span> </span>&#123;</span><br><span class="line">    balanceOf[msg.sender] = initialSupply;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>看一看合约代码右侧的下拉菜单，写着选择合约。选择“MyToken”合约后，你会看到现在这里显示了一个叫做构造函数参数的区域。他们是你合约里可以改变的参数，因此在未来你可以重用同样的合约代码只需要改变这些参数。</p>
<p><img src="https://www.ethereum.org/images/tutorial/function-picker.png" alt="image"></p>
<p>现在你有了一个合约可以创建代币的余额但还没有任何的函数能够转移它们，它们所能做的就是呆在同样的账户里。因此我们将实现转移函数。输入以下代码：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* Send coins */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">transfer</span>(<span class="params">address _to, uint256 _value</span>) </span>&#123;</span><br><span class="line">    <span class="comment">/* Add and subtract new balances */</span></span><br><span class="line">    balanceOf[msg.sender] -= _value;</span><br><span class="line">    balanceOf[_to] += _value;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>这是一个非常明确的函数：它有一个接收者跟一个value值作为参数，无论何时当用户调用这个函数，它将从用户的余额中减去_value，并且加到_to地址的余额里。现在这有一个明显的问题：假如用户想发送超过他们额度的代币呢？因此我们不想在这个合约里来处理债务的问题，所以我们简单的快速检查一下如果用户的资金不够那么合同的执行就会停止。同样要检查以下溢出，避免一个很大的数出现后溢出，账户的余额变成0。</p>
<p>在合约的执行过程中停止一个合约的执行你能使用<strong>return</strong>跟<strong>throw</strong>，这种形式会花费很少的gas但会让人头疼，因为你对合约所做的任何改变都会被保存。另一方面，‘throw’会终止所有合约的执行，恢复所有交易做出的改变，并且调用者会损失这些gas。但因为钱包会侦测到一个合约会出现throw，它会显示一个警告，防止任何以太被花费。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">transfer</span>(<span class="params">address _to, uint256 _value</span>) </span>&#123;</span><br><span class="line">    <span class="comment">/* Check if sender has balance and for overflows */</span></span><br><span class="line">    <span class="built_in">require</span>(balanceOf[msg.sender] &gt;= _value &amp;&amp; balanceOf[_to] + _value &gt;= balanceOf[_to]);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Add and subtract new balances */</span></span><br><span class="line">    balanceOf[msg.sender] -= _value;</span><br><span class="line">    balanceOf[_to] += _value;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>现在缺少一些合同的基本的信息。在未来这些信息能够在代币注册中编辑到，但现在我们将它们直接添加到合约中：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">string public name;</span><br><span class="line">string public symbol;</span><br><span class="line">uint8 public decimals;</span><br></pre></td></tr></table></figure>

<p>现在我们更新构造函数来允许所有这些变量能够在初始化的时候被设置：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* Initializes contract with initial supply tokens to the creator of the contract */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">MyToken</span>(<span class="params">uint256 initialSupply, string tokenName, string tokenSymbol, uint8 decimalUnits</span>) </span>&#123;</span><br><span class="line">    balanceOf[msg.sender] = initialSupply;              <span class="comment">// Give the creator all initial tokens</span></span><br><span class="line">    name = tokenName;                                   <span class="comment">// Set the name for display purposes</span></span><br><span class="line">    symbol = tokenSymbol;                               <span class="comment">// Set the symbol for display purposes</span></span><br><span class="line">    decimals = decimalUnits;                            <span class="comment">// Amount of decimals for display purposes</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>最后，我们现在需要某些叫做时间Events的东西。它们是特殊的，空的方法，你能调用来帮助客户端比如以太坊钱包来跟踪合约的活动事件。事件应该由一个大写字母开头。将这行代码添加到合约的开始处来声明这个事件：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">event Transfer(address indexed <span class="keyword">from</span>, address indexed to, uint256 value);</span><br></pre></td></tr></table></figure>

<p>现在你只需要增加两行在transfer函数里：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* Notify anyone listening that this transfer took place */</span></span><br><span class="line">Transfer(msg.sender, _to, _value);</span><br></pre></td></tr></table></figure>

<p>现在你的代币已经准备好了！</p>
<h3 id="注意到了注释？"><a href="#注意到了注释？" class="headerlink" title="注意到了注释？"></a>注意到了注释？</h3><p>那些@notice与@param注释是什么意思呢？你可能会问。那是自然语言定义的一些新的标准，用来让钱包显示给用户一个自然语言的描述来解释合约的用途。尽管当前还没有被很多钱包所支持，但未来会有所改变，所以最好还是加上。</p>
<h3 id="如何部署合约"><a href="#如何部署合约" class="headerlink" title="如何部署合约"></a>如何部署合约</h3><p>如果还没有准备好，打开以太坊钱包，去到合约页面点击“部署新合约”</p>
<p>现在从上面获取代币的源代码粘贴进“SOLIDITY源代码区域”。如果代码的编译没有任何错误，你应该会看到右侧的一个“选择合约”下拉列表。选择“MyToken”合约。在右侧，你将看到所有你的代币的个性化定制参数。你能随意调整它们，但根据这篇教程的意图，我们推荐你使用以下参数：总数设为10000，代币的名字随意，%为标志，2个小数位。你的钱包应该这样显示：</p>
<p><img src="https://www.ethereum.org/images/tutorial/Ethereum-Wallet-Screenshot-2015-12-03-at-3.50.36-PM-10.png" alt="image"></p>
<p>滚动到页面的底部你会看到部署合约的估算价格，你可以选择你自己想支付的费用。任何多余的以太坊将会返还给你，如果你愿意的话，你可以使用默认的设置。点击“部署”，输入你的账户密码等待一会，你的交易将被记录。</p>
<p><img src="https://www.ethereum.org/images/tutorial/Ethereum-Wallet-Screenshot-2015-12-03-at-3.50.36-PM-11.png" alt="image"></p>
<p>然后钱包会跳转到前面的页面，你能够看到你的交易正在等待确认。点击账户“Etherbase”（你的主账户），一分钟后你会看到你的账户拥有你刚刚创建的100%代币份额。发送一些给你的朋友：选择“发送”，选择哪种货币你希望发送（以太坊或者你刚刚创建的），粘贴你朋友的地址到“发送至”区域然后点击“发送”。</p>
<p><img src="https://www.ethereum.org/images/tutorial/Screen-Shot-2015-12-03-at-9.48.15-AM.png" alt="image"></p>
<p>如果你发送了代币给你的朋友，他们在钱包里是看不到任何东西的。因为你必须手动的添加。现在去到“合约”页面你会看到你刚刚创建的合约的链接。点击它进入一个合约页面，这是一个非常简单的合约页面，点击“复制地址”粘贴到文本编辑器里，等会会需要这个。</p>
<p>添加一个代币来观察，进入合约页面点击“查看代币”。出现一个弹出窗口，你只需要粘贴合约地址。代币的名字，缩写符号跟小数位会自动填充进来。如果没有自动填写，你可以修改成任意你想要的值（只是会影响你自己的钱包怎么显示）。一旦你完成这些，你就能够自动的显示这些代币的余额同时你能够发送它们给任何其他人。</p>
<p><img src="https://www.ethereum.org/images/tutorial/Screen-Shot-2015-12-03-at-9.44.42-AM.png" alt="image"></p>
<p>现在你拥有了你自己的加密代币！代币本身能够被用作本地社区的价值交换，比如作为追踪工人的工作时间与其它绩效考核项目。但我们可以让一个货币拥有自己的价值使它更有用？</p>
<h2 id="改进你的代币"><a href="#改进你的代币" class="headerlink" title="改进你的代币"></a>改进你的代币</h2><p>你能够发布你的加密货币而不需要碰任何一行代码，但真正的魔力在于当你开始定制这些代币。下面这段内容将会是一些建议关于增加你的代币的功能来满足你的更多需求。</p>
<h2 id="更多基础功能"><a href="#更多基础功能" class="headerlink" title="更多基础功能"></a>更多基础功能</h2><p>你会注意到这儿有一些其它的方法在你的代币合约中，比如approve，sendFrom与其它。这些函数在那帮助你的代币与其它合约进行交互：如果你希望这样，比如说，卖一些代币给一个去中心化的交易所，只是发送给他们一个地址是不够的，因为交易所不会意识到新的代币或者是谁发送它们，因为合约不能通过<strong>订阅事件</strong>来调用函数。所以对于合约，你应该首先批准它们能转移的代币数量然后ping通知他们让他们知道他们应该做自己的事情或者将这两个动作合二为一，叫做<strong>approveAndCall</strong>。</p>
<p>因为许多函数都将不得不重新实现代币的传输，将其变为内部函数是有意义的，这样智能被合约自己内部调用：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* Internal transfer, can only be called by this contract */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">_transfer</span>(<span class="params">address _from, address _to, uint _value</span>) <span class="title">internal</span> </span>&#123;</span><br><span class="line">    <span class="built_in">require</span> (_to != <span class="number">0x0</span>);                               <span class="comment">// Prevent transfer to 0x0 address. Use burn() instead</span></span><br><span class="line">    <span class="built_in">require</span> (balanceOf[_from] &gt;= _value);                <span class="comment">// Check if the sender has enough</span></span><br><span class="line">    <span class="built_in">require</span> (balanceOf[_to] + _value &gt; balanceOf[_to]); <span class="comment">// Check for overflows</span></span><br><span class="line">    <span class="built_in">require</span>(!frozenAccount[_from]);                     <span class="comment">// Check if sender is frozen</span></span><br><span class="line">    <span class="built_in">require</span>(!frozenAccount[_to]);                       <span class="comment">// Check if recipient is frozen</span></span><br><span class="line">    balanceOf[_from] -= _value;                         <span class="comment">// Subtract from the sender</span></span><br><span class="line">    balanceOf[_to] += _value;                           <span class="comment">// Add the same to the recipient</span></span><br><span class="line">    Transfer(_from, _to, _value);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>现在所有你的能够发起代币传输的函数，能够自己做检查然后使用正确的参数来调用transfer。注意这个函数将会移动金币从任何一个账户到另一个账户，不需要任何的权限来执行：这就是为什么这是一个内部函数，智能在合约内部被调用：如果你添加一些函数来调用它，确保应该适当的验证调用者是否有权限。</p>
<h3 id="集中管理"><a href="#集中管理" class="headerlink" title="集中管理"></a>集中管理</h3><p>所有的dapp默认都是完全去中心化的，但这并不意味着他们就不能拥有某些中央管理者。也许你想拥有某些铸币的能力，也许你想禁止某人使用你的货币。你能够任意添加这些功能，但问题是你只能够在一开始就添加它们，因此所有的代币拥有者在拥有这些代币之前都会清楚知道游戏的规则。</p>
<p>要那样做的话，你需要一个货币的中央控制者。这可以是一个简单的账户，但同样可以是一个合约，因此创建更多代币的决议将会取决于这个合约：如果它是一个民主的组织他们可以投票选举，或许这会是限制代币拥有者权力的一种方式。</p>
<p>达到这样的目的我们将会学习一个关于合约的非常有用的性质：<strong>继承</strong>。继承允许一个合约获得它的父合约的属性，这样就不需要重新定义它们。这使得代码清晰整洁并且易于重用。加入这段代码在第一行，在MyToken {之前。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">contract owned &#123;</span><br><span class="line">    address public owner;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">owned</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        owner = msg.sender;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    modifier onlyOwner &#123;</span><br><span class="line">        <span class="built_in">require</span>(msg.sender == owner);</span><br><span class="line">        _;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">transferOwnership</span>(<span class="params">address newOwner</span>) <span class="title">onlyOwner</span> </span>&#123;</span><br><span class="line">        owner = newOwner;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这里创建了一个非常基础的合约，除了定义了一些通用的函数关于合约能够被“owned”之外不做其它事情。接下来一步就是把这段代码加到你的合约里：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">contract MyToken is owned &#123;</span><br><span class="line">    <span class="comment">/* the rest of the contract as usual */</span></span><br></pre></td></tr></table></figure>

<p>这意味着所有在MyToken里的函数能够访问owenr变量以及onlyOwner修改器。这个合约同样获得了一个函数来转移ownership。因为在启动的时候来设置合约的拥有着可能比较有趣，你同样可以添加进构造函数：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">MyToken</span>(<span class="params"></span></span></span><br><span class="line"><span class="params"><span class="function">    uint256 initialSupply,</span></span></span><br><span class="line"><span class="params"><span class="function">    string tokenName,</span></span></span><br><span class="line"><span class="params"><span class="function">    uint8 decimalUnits,</span></span></span><br><span class="line"><span class="params"><span class="function">    string tokenSymbol,</span></span></span><br><span class="line"><span class="params"><span class="function">    address centralMinter</span></span></span><br><span class="line"><span class="params"><span class="function">    </span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(centralMinter != <span class="number">0</span> ) owner = centralMinter;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="中心化铸币"><a href="#中心化铸币" class="headerlink" title="中心化铸币"></a>中心化铸币</h3><p>设想你希望改变已经流通的货币数量。比如当你的代币代表了一个区块链资产（比如黄金券或者政府的法币），你希望你的虚拟资产能够反映真实的数量。这同样会是一种情况当货币拥有着希望对代币进行价格上的控制，增发或者从流通的货币中废除一些代币。</p>
<p>首先，我们需要添加一个变量来存储代币总量，同时在构造函数里赋值。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">contract MyToken &#123;</span><br><span class="line">    uint256 public totalSupply;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">MyToken</span>(<span class="params">...</span>) </span>&#123;</span><br><span class="line">        totalSupply = initialSupply;</span><br><span class="line">        ...</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>现在让我们增加一个新的函数来允许合约拥有着来创建新的代币：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">mintToken</span>(<span class="params">address target, uint256 mintedAmount</span>) <span class="title">onlyOwner</span> </span>&#123;</span><br><span class="line">    balanceOf[target] += mintedAmount;</span><br><span class="line">    totalSupply += mintedAmount;</span><br><span class="line">    Transfer(<span class="number">0</span>, owner, mintedAmount);</span><br><span class="line">    Transfer(owner, target, mintedAmount);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意这个在函数名后面的onlyOwner修改器。这是指这个函数会在编译的时候将被重写，到继承我们之前定义的onlyOwner修改器。这个函数的代码将会被插入到修改器的函数中，位于下划线的地方，意味着这个特别的函数只能设置为owner的账户所调用。将这段代码其添加到有owner修改器的合约中就能实现铸币功能。</p>
<h3 id="资产冻结"><a href="#资产冻结" class="headerlink" title="资产冻结"></a>资产冻结</h3><p>根据你自己的用例，你可能有一些管理上的障碍，比如谁能使用谁不能使用你的代币。为了实现这个目的，你可以添加一个参数来允许合约拥有者能够冻结跟解冻资产。</p>
<p>在合约里的任意位置添加这个变量和函数。你可以放在任何位置但我们推荐你把映射跟事件跟其它事件映射放在一起。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">mapping (<span class="function"><span class="params">address</span> =&gt;</span> bool) public frozenAccount;</span><br><span class="line">event FrozenFunds(address target, bool frozen);</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">freezeAccount</span>(<span class="params">address target, bool freeze</span>) <span class="title">onlyOwner</span> </span>&#123;</span><br><span class="line">    frozenAccount[target] = freeze;</span><br><span class="line">    FrozenFunds(target, freeze);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>有了这段代码，所有的账户都是默认未冻结的但合约的拥有者可以设置账户中的任意一个为冻结状态通过调用freezeAccount。不过，冻结功能还不能发挥实际效果因为我们还没有添加相关内容到transfer函数。我们现在来修改一下：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">transfer</span>(<span class="params">address _to, uint256 _value</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">require</span>(!frozenAccount[msg.sender]);</span><br></pre></td></tr></table></figure>
<p>现在任何被冻结的账户任然拥有他们完整的资产，但不能动用他们的资产了。所有的账户默认都是未冻结的直到你冻结他们，但你能轻易的恢复这些行为在白名单里，你需要手动的批准每个账号。重命名frozenAccount函数为approveAccount并且修改最后一行为：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">require</span>(approvedAccount[msg.sender]);</span><br></pre></td></tr></table></figure>

<h3 id="自动买卖"><a href="#自动买卖" class="headerlink" title="自动买卖"></a>自动买卖</h3><p>到目前为止，你已经基于工具和信任来为你的代币赋值。但如果你想你的代币由以太坊（或者其它代币）来站台，创建一个基金可以自动的按市场价格来进行买卖你的代币。</p>
<p>首先我们设置价格来进行买卖：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">uint256 public sellPrice;</span><br><span class="line">uint256 public buyPrice;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">setPrices</span>(<span class="params">uint256 newSellPrice, uint256 newBuyPrice</span>) <span class="title">onlyOwner</span> </span>&#123;</span><br><span class="line">    sellPrice = newSellPrice;</span><br><span class="line">    buyPrice = newBuyPrice;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>价格不会被频繁修改是可以被接收的，因为每次新价格的改变会需要你执行一次交易并且花费一点以太坊。如果你想有用一个恒定浮动的价格我们推荐你调查standard data feeds</p>
<p>下一步是创建买卖函数：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">buy</span>(<span class="params"></span>) <span class="title">payable</span> <span class="title">returns</span> (<span class="params">uint amount</span>)</span>&#123;</span><br><span class="line">    amount = msg.value / buyPrice;                    <span class="comment">// calculates the amount</span></span><br><span class="line">    <span class="built_in">require</span>(balanceOf[<span class="built_in">this</span>] &gt;= amount);               <span class="comment">// checks if it has enough to sell</span></span><br><span class="line">    balanceOf[msg.sender] += amount;                  <span class="comment">// adds the amount to buyer&#x27;s balance</span></span><br><span class="line">    balanceOf[<span class="built_in">this</span>] -= amount;                        <span class="comment">// subtracts amount from seller&#x27;s balance</span></span><br><span class="line">    Transfer(<span class="built_in">this</span>, msg.sender, amount);               <span class="comment">// execute an event reflecting the change</span></span><br><span class="line">    <span class="keyword">return</span> amount;                                    <span class="comment">// ends function and returns</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sell</span>(<span class="params">uint amount</span>) <span class="title">returns</span> (<span class="params">uint revenue</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">require</span>(balanceOf[msg.sender] &gt;= amount);         <span class="comment">// checks if the sender has enough to sell</span></span><br><span class="line">    balanceOf[<span class="built_in">this</span>] += amount;                        <span class="comment">// adds the amount to owner&#x27;s balance</span></span><br><span class="line">    balanceOf[msg.sender] -= amount;                  <span class="comment">// subtracts the amount from seller&#x27;s balance</span></span><br><span class="line">    revenue = amount * sellPrice;</span><br><span class="line">    msg.sender.transfer(revenue);                     <span class="comment">// sends ether to the seller: it&#x27;s important to do this last to prevent recursion attacks</span></span><br><span class="line">    Transfer(msg.sender, <span class="built_in">this</span>, amount);               <span class="comment">// executes an event reflecting on the change</span></span><br><span class="line">    <span class="keyword">return</span> revenue;                                   <span class="comment">// ends function and returns</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意这不会创建新的代币只是更改合约拥有的余额。合约能够同时拥有它自己的代币数量，以太数以及合约的拥有者，当它能够设置价格或者在某些情况可以创建新的代币（如果可以），它并不会本来的代币跟以太。此合同能转移资金的唯一方式就是买卖它们。</p>
<p>注意买卖的价格单位不是以太，是wei，这个是整个系统的最小货币单位（相当于欧元跟美元里的分，或者比特币里面的聪）。一个以太等于1000000000000000000 wei。所以当你设置你代币的价格的时候，需要在末尾添加18个零。</p>
<p>当创建合约时，要发送足够的以太，这样它能在市场上买回所有的代币，要不然你的合同就会破产，你的用户将不能出售它们的代币。</p>
<p>当然前面的例子描述的是一个单一的中心化的买家和卖家的合约。更有趣的合约会允许任何人出价，或者从外部直接报价的市场。</p>
<h3 id="自动重填"><a href="#自动重填" class="headerlink" title="自动重填"></a>自动重填</h3><p>每一次，你在以太坊进行一次交易，都需要支付一笔费用给打包区块的矿工它们会计算智能合约的结果。这可能会在未来改变，但当前费用只能被以以太支付，因此所有你的代币的拥有者都需要以太。账户的代币余额如果小于这笔费用会被暂时卡住直到用户能够支付交易费。但在一些情况里，你可能不希望你的用户去考虑以太坊，区块链以及怎样获取以太，所以一种可能是让你的代币可以自动重新填充用户的余额当它发现用户的余额低的比较危险。</p>
<p>为了做到这个，首先你需要创建一个变量来存储阈值以及一个函数来修改这个变量。如果你不知道这个值是多少，将它设置为5 finney (0.005 ether).</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">uint minBalanceForAccounts;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">setMinBalance</span>(<span class="params">uint minimumBalanceInFinney</span>) <span class="title">onlyOwner</span> </span>&#123;</span><br><span class="line">     minBalanceForAccounts = minimumBalanceInFinney * <span class="number">1</span> finney;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>接着，添加这行代码到交易函数，这样能确保发送者得到退款：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* Send coins */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">transfer</span>(<span class="params">address _to, uint256 _value</span>) </span>&#123;</span><br><span class="line">    ...</span><br><span class="line">    <span class="keyword">if</span>(msg.sender.balance &lt; minBalanceForAccounts)</span><br><span class="line">        sell((minBalanceForAccounts - msg.sender.balance) / sellPrice);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>你也可以更改它让这笔费用由发送方支付给接收者：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* Send coins */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">transfer</span>(<span class="params">address _to, uint256 _value</span>) </span>&#123;</span><br><span class="line">    ...</span><br><span class="line">    <span class="keyword">if</span>(_to.balance&lt;minBalanceForAccounts)</span><br><span class="line">        _to.send(sell((minBalanceForAccounts - _to.balance) / sellPrice));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="工作量证明"><a href="#工作量证明" class="headerlink" title="工作量证明"></a>工作量证明</h3><p>这里有一些方法来将你的代币供应绑定到一个数学的公式上。其中一个最简单的方式叫做与以太结合的“混合挖矿”，意思是任何人谁找到了以太坊的一个新区块将同样获得你的代币的奖励，开放给任何人调用奖励函数在新区块上。你能够使用特别的关键字coinbase用来表示发现新区快的矿工。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">function giveBlockReward() &#123;</span><br><span class="line">    balanceOf[block.coinbase] += 1;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>同样也可以添加一个数学公式，这样任何人谁能解开数学公式就能获得奖励。在接下来的例子中你必须计算立方根：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">uint currentChallenge = <span class="number">1</span>; <span class="comment">// Can you figure out the cubic root of this number?</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">rewardMathGeniuses</span>(<span class="params">uint answerToCurrentReward, uint nextChallenge</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">require</span>(answerToCurrentReward**<span class="number">3</span> == currentChallenge); <span class="comment">// If answer is wrong do not continue</span></span><br><span class="line">    balanceOf[msg.sender] += <span class="number">1</span>;         <span class="comment">// Reward the player</span></span><br><span class="line">    currentChallenge = nextChallenge;   <span class="comment">// Set the next challenge</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>当然，计算立方根对于某人用大脑来说的话比较难，但对于计算器来说非常简单，所以这个游戏会被计算机轻松破解。同样最后的胜利者可以选择下一个挑战，他们能选择他们熟悉的一些东西所以对所有玩家来说可能并不是很公平。这有一些任务可能对于人类来说比较简单但对计算机比较难但通常会比较难以编码。取而代之，一个公平的系统应该是对于计算机来说难于计算的，但容易验证结果。一个不错的选择就是创建一个hash竞赛，然后参与者必须从多个数字中算出hash值直到他们能找到一个比给定难度低的值。</p>
<p>这一过程首先被Adam Back在1997年设计Hashcash的时候提出同样在2008年中本聪的比特币将其作为工作量证明的算实现。以太坊项目启动的时候使用了这个系统作为它的安全模型，但未来将从工作量证明安全模型转移到混合权益证明和一个经常叫做Casper的系统。</p>
<p>但如果你喜欢使用hash算法作为随机发行代币的一种方式，你能够继续创建你自己的基于工作量证明发行的以太坊上的货币：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">bytes32 public currentChallenge;                         <span class="comment">// The coin starts with a challenge</span></span><br><span class="line">uint public timeOfLastProof;                             <span class="comment">// Variable to keep track of when rewards were given</span></span><br><span class="line">uint public difficulty = <span class="number">10</span>**<span class="number">32</span>;                         <span class="comment">// Difficulty starts reasonably low</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">proofOfWork</span>(<span class="params">uint nonce</span>)</span>&#123;</span><br><span class="line">    bytes8 n = bytes8(sha3(nonce, currentChallenge));    <span class="comment">// Generate a random hash based on input</span></span><br><span class="line">    <span class="built_in">require</span>(n &gt;= bytes8(difficulty));                   <span class="comment">// Check if it&#x27;s under the difficulty</span></span><br><span class="line"></span><br><span class="line">    uint timeSinceLastProof = (now - timeOfLastProof);  <span class="comment">// Calculate time since last reward was given</span></span><br><span class="line">    <span class="built_in">require</span>(timeSinceLastProof &gt;=  <span class="number">5</span> seconds);         <span class="comment">// Rewards cannot be given too quickly</span></span><br><span class="line">    balanceOf[msg.sender] += timeSinceLastProof / <span class="number">60</span> seconds;  <span class="comment">// The reward to the winner grows by the minute</span></span><br><span class="line"></span><br><span class="line">    difficulty = difficulty * <span class="number">10</span> minutes / timeSinceLastProof + <span class="number">1</span>;  <span class="comment">// Adjusts the difficulty</span></span><br><span class="line"></span><br><span class="line">    timeOfLastProof = now;                              <span class="comment">// Reset the counter</span></span><br><span class="line">    currentChallenge = sha3(nonce, currentChallenge, block.blockhash(block.number - <span class="number">1</span>));  <span class="comment">// Save a hash that will be used as the next proof</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>同样改变构造函数，增加这行代码，所以难度调整将不会很疯狂：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">timeOfLastProof = now;</span><br></pre></td></tr></table></figure>

<h2 id="改进的代币"><a href="#改进的代币" class="headerlink" title="改进的代币"></a>改进的代币</h2><h3 id="完整代码"><a href="#完整代码" class="headerlink" title="完整代码"></a>完整代码</h3><p>如果你添加了所有这些高级的属性，那么代码应该看上去像这样：</p>
<p><img src="https://www.ethereum.org/images/tutorial/advanced-token-deploy.png" alt="image"></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br></pre></td><td class="code"><pre><span class="line">pragma solidity ^<span class="number">0.4</span><span class="number">.16</span>;</span><br><span class="line"></span><br><span class="line">contract owned &#123;</span><br><span class="line">    address public owner;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">owned</span>(<span class="params"></span>) <span class="title">public</span> </span>&#123;</span><br><span class="line">        owner = msg.sender;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    modifier onlyOwner &#123;</span><br><span class="line">        <span class="built_in">require</span>(msg.sender == owner);</span><br><span class="line">        _;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">transferOwnership</span>(<span class="params">address newOwner</span>) <span class="title">onlyOwner</span> <span class="title">public</span> </span>&#123;</span><br><span class="line">        owner = newOwner;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">interface tokenRecipient &#123; <span class="function"><span class="keyword">function</span> <span class="title">receiveApproval</span>(<span class="params">address _from, uint256 _value, address _token, bytes _extraData</span>) <span class="title">public</span></span>; &#125;</span><br><span class="line"></span><br><span class="line">contract TokenERC20 &#123;</span><br><span class="line">    <span class="comment">// Public variables of the token</span></span><br><span class="line">    string public name;</span><br><span class="line">    string public symbol;</span><br><span class="line">    uint8 public decimals = <span class="number">18</span>;</span><br><span class="line">    <span class="comment">// 18 decimals is the strongly suggested default, avoid changing it</span></span><br><span class="line">    uint256 public totalSupply;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// This creates an array with all balances</span></span><br><span class="line">    mapping (<span class="function"><span class="params">address</span> =&gt;</span> uint256) public balanceOf;</span><br><span class="line">    mapping (<span class="function"><span class="params">address</span> =&gt;</span> mapping (<span class="function"><span class="params">address</span> =&gt;</span> uint256)) public allowance;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// This generates a public event on the blockchain that will notify clients</span></span><br><span class="line">    event Transfer(address indexed <span class="keyword">from</span>, address indexed to, uint256 value);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// This notifies clients about the amount burnt</span></span><br><span class="line">    event Burn(address indexed <span class="keyword">from</span>, uint256 value);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Constrctor function</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * Initializes contract with initial supply tokens to the creator of the contract</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">TokenERC20</span>(<span class="params"></span></span></span><br><span class="line"><span class="params"><span class="function">        uint256 initialSupply,</span></span></span><br><span class="line"><span class="params"><span class="function">        string tokenName,</span></span></span><br><span class="line"><span class="params"><span class="function">        string tokenSymbol</span></span></span><br><span class="line"><span class="params"><span class="function">    </span>) <span class="title">public</span> </span>&#123;</span><br><span class="line">        totalSupply = initialSupply * <span class="number">10</span> ** uint256(decimals);  <span class="comment">// Update total supply with the decimal amount</span></span><br><span class="line">        balanceOf[msg.sender] = totalSupply;                <span class="comment">// Give the creator all initial tokens</span></span><br><span class="line">        name = tokenName;                                   <span class="comment">// Set the name for display purposes</span></span><br><span class="line">        symbol = tokenSymbol;                               <span class="comment">// Set the symbol for display purposes</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Internal transfer, only can be called by this contract</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">_transfer</span>(<span class="params">address _from, address _to, uint _value</span>) <span class="title">internal</span> </span>&#123;</span><br><span class="line">        <span class="comment">// Prevent transfer to 0x0 address. Use burn() instead</span></span><br><span class="line">        <span class="built_in">require</span>(_to != <span class="number">0x0</span>);</span><br><span class="line">        <span class="comment">// Check if the sender has enough</span></span><br><span class="line">        <span class="built_in">require</span>(balanceOf[_from] &gt;= _value);</span><br><span class="line">        <span class="comment">// Check for overflows</span></span><br><span class="line">        <span class="built_in">require</span>(balanceOf[_to] + _value &gt; balanceOf[_to]);</span><br><span class="line">        <span class="comment">// Save this for an assertion in the future</span></span><br><span class="line">        uint previousBalances = balanceOf[_from] + balanceOf[_to];</span><br><span class="line">        <span class="comment">// Subtract from the sender</span></span><br><span class="line">        balanceOf[_from] -= _value;</span><br><span class="line">        <span class="comment">// Add the same to the recipient</span></span><br><span class="line">        balanceOf[_to] += _value;</span><br><span class="line">        Transfer(_from, _to, _value);</span><br><span class="line">        <span class="comment">// Asserts are used to use static analysis to find bugs in your code. They should never fail</span></span><br><span class="line">        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Transfer tokens</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * Send `_value` tokens to `_to` from your account</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_to The address of the recipient</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_value the amount to send</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">transfer</span>(<span class="params">address _to, uint256 _value</span>) <span class="title">public</span> </span>&#123;</span><br><span class="line">        _transfer(msg.sender, _to, _value);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Transfer tokens from other address</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * Send `_value` tokens to `_to` in behalf of `_from`</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_from The address of the sender</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_to The address of the recipient</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_value the amount to send</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">transferFrom</span>(<span class="params">address _from, address _to, uint256 _value</span>) <span class="title">public</span> <span class="title">returns</span> (<span class="params">bool success</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">require</span>(_value &lt;= allowance[_from][msg.sender]);     <span class="comment">// Check allowance</span></span><br><span class="line">        allowance[_from][msg.sender] -= _value;</span><br><span class="line">        _transfer(_from, _to, _value);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Set allowance for other address</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * Allows `_spender` to spend no more than `_value` tokens in your behalf</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_spender The address authorized to spend</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_value the max amount they can spend</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">approve</span>(<span class="params">address _spender, uint256 _value</span>) <span class="title">public</span></span></span><br><span class="line"><span class="function">        <span class="title">returns</span> (<span class="params">bool success</span>) </span>&#123;</span><br><span class="line">        allowance[msg.sender][_spender] = _value;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Set allowance for other address and notify</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * Allows `_spender` to spend no more than `_value` tokens in your behalf, and then ping the contract about it</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_spender The address authorized to spend</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_value the max amount they can spend</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_extraData some extra information to send to the approved contract</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">approveAndCall</span>(<span class="params">address _spender, uint256 _value, bytes _extraData</span>)</span></span><br><span class="line"><span class="function">        <span class="title">public</span></span></span><br><span class="line"><span class="function">        <span class="title">returns</span> (<span class="params">bool success</span>) </span>&#123;</span><br><span class="line">        tokenRecipient spender = tokenRecipient(_spender);</span><br><span class="line">        <span class="keyword">if</span> (approve(_spender, _value)) &#123;</span><br><span class="line">            spender.receiveApproval(msg.sender, _value, <span class="built_in">this</span>, _extraData);</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Destroy tokens</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * Remove `_value` tokens from the system irreversibly</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_value the amount of money to burn</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">burn</span>(<span class="params">uint256 _value</span>) <span class="title">public</span> <span class="title">returns</span> (<span class="params">bool success</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">require</span>(balanceOf[msg.sender] &gt;= _value);   <span class="comment">// Check if the sender has enough</span></span><br><span class="line">        balanceOf[msg.sender] -= _value;            <span class="comment">// Subtract from the sender</span></span><br><span class="line">        totalSupply -= _value;                      <span class="comment">// Updates totalSupply</span></span><br><span class="line">        Burn(msg.sender, _value);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Destroy tokens from other account</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * Remove `_value` tokens from the system irreversibly on behalf of `_from`.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_from the address of the sender</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param </span>_value the amount of money to burn</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">burnFrom</span>(<span class="params">address _from, uint256 _value</span>) <span class="title">public</span> <span class="title">returns</span> (<span class="params">bool success</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">require</span>(balanceOf[_from] &gt;= _value);                <span class="comment">// Check if the targeted balance is enough</span></span><br><span class="line">        <span class="built_in">require</span>(_value &lt;= allowance[_from][msg.sender]);    <span class="comment">// Check allowance</span></span><br><span class="line">        balanceOf[_from] -= _value;                         <span class="comment">// Subtract from the targeted balance</span></span><br><span class="line">        allowance[_from][msg.sender] -= _value;             <span class="comment">// Subtract from the sender&#x27;s allowance</span></span><br><span class="line">        totalSupply -= _value;                              <span class="comment">// Update totalSupply</span></span><br><span class="line">        Burn(_from, _value);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/******************************************/</span></span><br><span class="line"><span class="comment">/*       ADVANCED TOKEN STARTS HERE       */</span></span><br><span class="line"><span class="comment">/******************************************/</span></span><br><span class="line"></span><br><span class="line">contract MyAdvancedToken is owned, TokenERC20 &#123;</span><br><span class="line"></span><br><span class="line">    uint256 public sellPrice;</span><br><span class="line">    uint256 public buyPrice;</span><br><span class="line"></span><br><span class="line">    mapping (<span class="function"><span class="params">address</span> =&gt;</span> bool) public frozenAccount;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* This generates a public event on the blockchain that will notify clients */</span></span><br><span class="line">    event FrozenFunds(address target, bool frozen);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Initializes contract with initial supply tokens to the creator of the contract */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">MyAdvancedToken</span>(<span class="params"></span></span></span><br><span class="line"><span class="params"><span class="function">        uint256 initialSupply,</span></span></span><br><span class="line"><span class="params"><span class="function">        string tokenName,</span></span></span><br><span class="line"><span class="params"><span class="function">        string tokenSymbol</span></span></span><br><span class="line"><span class="params"><span class="function">    </span>) <span class="title">TokenERC20</span>(<span class="params">initialSupply, tokenName, tokenSymbol</span>) <span class="title">public</span> </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Internal transfer, only can be called by this contract */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">_transfer</span>(<span class="params">address _from, address _to, uint _value</span>) <span class="title">internal</span> </span>&#123;</span><br><span class="line">        <span class="built_in">require</span> (_to != <span class="number">0x0</span>);                               <span class="comment">// Prevent transfer to 0x0 address. Use burn() instead</span></span><br><span class="line">        <span class="built_in">require</span> (balanceOf[_from] &gt;= _value);               <span class="comment">// Check if the sender has enough</span></span><br><span class="line">        <span class="built_in">require</span> (balanceOf[_to] + _value &gt; balanceOf[_to]); <span class="comment">// Check for overflows</span></span><br><span class="line">        <span class="built_in">require</span>(!frozenAccount[_from]);                     <span class="comment">// Check if sender is frozen</span></span><br><span class="line">        <span class="built_in">require</span>(!frozenAccount[_to]);                       <span class="comment">// Check if recipient is frozen</span></span><br><span class="line">        balanceOf[_from] -= _value;                         <span class="comment">// Subtract from the sender</span></span><br><span class="line">        balanceOf[_to] += _value;                           <span class="comment">// Add the same to the recipient</span></span><br><span class="line">        Transfer(_from, _to, _value);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/// @notice Create `mintedAmount` tokens and send it to `target`</span></span><br><span class="line">    <span class="comment">/// @param target Address to receive the tokens</span></span><br><span class="line">    <span class="comment">/// @param mintedAmount the amount of tokens it will receive</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">mintToken</span>(<span class="params">address target, uint256 mintedAmount</span>) <span class="title">onlyOwner</span> <span class="title">public</span> </span>&#123;</span><br><span class="line">        balanceOf[target] += mintedAmount;</span><br><span class="line">        totalSupply += mintedAmount;</span><br><span class="line">        Transfer(<span class="number">0</span>, <span class="built_in">this</span>, mintedAmount);</span><br><span class="line">        Transfer(<span class="built_in">this</span>, target, mintedAmount);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/// @notice `freeze? Prevent | Allow` `target` from sending &amp; receiving tokens</span></span><br><span class="line">    <span class="comment">/// @param target Address to be frozen</span></span><br><span class="line">    <span class="comment">/// @param freeze either to freeze it or not</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">freezeAccount</span>(<span class="params">address target, bool freeze</span>) <span class="title">onlyOwner</span> <span class="title">public</span> </span>&#123;</span><br><span class="line">        frozenAccount[target] = freeze;</span><br><span class="line">        FrozenFunds(target, freeze);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/// @notice Allow users to buy tokens for `newBuyPrice` eth and sell tokens for `newSellPrice` eth</span></span><br><span class="line">    <span class="comment">/// @param newSellPrice Price the users can sell to the contract</span></span><br><span class="line">    <span class="comment">/// @param newBuyPrice Price users can buy from the contract</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">setPrices</span>(<span class="params">uint256 newSellPrice, uint256 newBuyPrice</span>) <span class="title">onlyOwner</span> <span class="title">public</span> </span>&#123;</span><br><span class="line">        sellPrice = newSellPrice;</span><br><span class="line">        buyPrice = newBuyPrice;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/// @notice Buy tokens from contract by sending ether</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">buy</span>(<span class="params"></span>) <span class="title">payable</span> <span class="title">public</span> </span>&#123;</span><br><span class="line">        uint amount = msg.value / buyPrice;               <span class="comment">// calculates the amount</span></span><br><span class="line">        _transfer(<span class="built_in">this</span>, msg.sender, amount);              <span class="comment">// makes the transfers</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/// @notice Sell `amount` tokens to contract</span></span><br><span class="line">    <span class="comment">/// @param amount amount of tokens to be sold</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">sell</span>(<span class="params">uint256 amount</span>) <span class="title">public</span> </span>&#123;</span><br><span class="line">        <span class="built_in">require</span>(<span class="built_in">this</span>.balance &gt;= amount * sellPrice);      <span class="comment">// checks if the contract has enough ether to buy</span></span><br><span class="line">        _transfer(msg.sender, <span class="built_in">this</span>, amount);              <span class="comment">// makes the transfers</span></span><br><span class="line">        msg.sender.transfer(amount * sellPrice);          <span class="comment">// sends ether to the seller. It&#x27;s important to do this last to avoid recursion attacks</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="发布"><a href="#发布" class="headerlink" title="发布"></a>发布</h3><p>滚动到最下面你会看到预估的部署成本价格。你也可以自己改变滑块来设置一个更少的费用，但如果价格比平均市场费率低太多的话你的交易会花费很长的时间打包。点击部署然后输入你的密码。不一会你会到控制面板最下面的交易里你会看到你的合约正在创建。等待交易被打包后，你的合于会创建成功。</p>
<p><img src="https://www.ethereum.org/images/tutorial/created-token.png" alt="image"></p>
<p>点击代币合约的管理页面链接，你将会来到世界上最简单的中央银行控制面板，你在这里对你的货币做任何你想做的事情。</p>
<p>在左侧的Readfrom contract下面你能从合约里读取信息，免费的。如果你的代币拥有一个拥有者，它会显示其地址。拷贝地址粘贴到balance of将会显示任意账户的余额（余额同样会自动显示在任何拥有这些代币的账户页面里）。</p>
<p>在右侧，在Write to Contract下，你会看到所有的函数你用来修改你的区块链，这些操作会消耗gas。如果你创建了一个允许挖矿的合约，你将拥有一个函数叫做“Mint Token”。选择它。</p>
<p><img src="https://www.ethereum.org/images/tutorial/manage-central-dollar.png" alt="image"></p>
<p>选择需要创建新的货币的地址，然后是数量（如果你的小数位设置为2，在金额的后面添加两个0，来创建正确的数量）。在<strong>Execute from</strong>选择设置为owener的账户，将以太的金额设置为0然后点击执行。</p>
<p>等待一些确认后，接收者的余额会更新为新的金额。但接收者的钱包可能没法自动显示：设置查看代币。</p>
<p><img src="https://www.ethereum.org/images/tutorial/add-token.png" alt="image"></p>
<h2 id="使用你的代币"><a href="#使用你的代币" class="headerlink" title="使用你的代币"></a>使用你的代币</h2><p>一旦你发布了你的代币，他们会被添加到你的被观察代币的列表里，代币余额会显示在你的账户里。如果要发送代币，去发送界面选择一个有代币的账户，选择一个并且输入你想要发送的代币的数量。</p>
<p>如果你希望添加其它人的代币，转到合约页面，点击观察代币。比如，添加独角兽（🦄）代币到你的观察列表里，添加地址0x89205A3A3b2A69De6Dbf7f01ED13B2108B2c43e7，剩下的信息会自动载入进来。点击OK你的代币会被添加进来。</p>
<p><img src="https://www.ethereum.org/images/tutorial/unicorn-token.png" alt="image"></p>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item"></div>
      <div class="post-nav-item">
    <a href="/2021/09/29/chuang-jian-jia-mi-mao/" rel="next" title="如何在以太网上创建你自己的加密猫游戏">
      如何在以太网上创建你自己的加密猫游戏 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%A3%E5%B8%81"><span class="nav-number">1.</span> <span class="nav-text">代币</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9C%80%E5%B0%8F%E5%8F%AF%E8%A1%8C%E4%BB%A3%E5%B8%81"><span class="nav-number">1.1.</span> <span class="nav-text">最小可行代币</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%A3%E7%A0%81"><span class="nav-number">1.2.</span> <span class="nav-text">代码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%90%86%E8%A7%A3%E4%BB%A3%E7%A0%81"><span class="nav-number">1.3.</span> <span class="nav-text">理解代码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B3%A8%E6%84%8F%E5%88%B0%E4%BA%86%E6%B3%A8%E9%87%8A%EF%BC%9F"><span class="nav-number">1.4.</span> <span class="nav-text">注意到了注释？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A6%82%E4%BD%95%E9%83%A8%E7%BD%B2%E5%90%88%E7%BA%A6"><span class="nav-number">1.5.</span> <span class="nav-text">如何部署合约</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%94%B9%E8%BF%9B%E4%BD%A0%E7%9A%84%E4%BB%A3%E5%B8%81"><span class="nav-number">2.</span> <span class="nav-text">改进你的代币</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9B%B4%E5%A4%9A%E5%9F%BA%E7%A1%80%E5%8A%9F%E8%83%BD"><span class="nav-number">3.</span> <span class="nav-text">更多基础功能</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%9B%86%E4%B8%AD%E7%AE%A1%E7%90%86"><span class="nav-number">3.1.</span> <span class="nav-text">集中管理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%AD%E5%BF%83%E5%8C%96%E9%93%B8%E5%B8%81"><span class="nav-number">3.2.</span> <span class="nav-text">中心化铸币</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%B5%84%E4%BA%A7%E5%86%BB%E7%BB%93"><span class="nav-number">3.3.</span> <span class="nav-text">资产冻结</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%87%AA%E5%8A%A8%E4%B9%B0%E5%8D%96"><span class="nav-number">3.4.</span> <span class="nav-text">自动买卖</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%87%AA%E5%8A%A8%E9%87%8D%E5%A1%AB"><span class="nav-number">3.5.</span> <span class="nav-text">自动重填</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B7%A5%E4%BD%9C%E9%87%8F%E8%AF%81%E6%98%8E"><span class="nav-number">3.6.</span> <span class="nav-text">工作量证明</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%94%B9%E8%BF%9B%E7%9A%84%E4%BB%A3%E5%B8%81"><span class="nav-number">4.</span> <span class="nav-text">改进的代币</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81"><span class="nav-number">4.1.</span> <span class="nav-text">完整代码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%91%E5%B8%83"><span class="nav-number">4.2.</span> <span class="nav-text">发布</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E4%BD%A0%E7%9A%84%E4%BB%A3%E5%B8%81"><span class="nav-number">5.</span> <span class="nav-text">使用你的代币</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">李工</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">4</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">李工</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://mist.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
